package com.ap.core.jbehave.managemerchant;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.IOException;

import javax.xml.ws.soap.SOAPFaultException;

import org.apache.commons.io.IOUtils;
import org.hamcrest.CoreMatchers;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.ap.core.cxf.jaxb.common.ActivationStatus;
import com.ap.core.cxf.jaxb.common.RequestTypeCode;
import com.ap.core.cxf.jaxb.common.UpdateStatus;
import com.ap.core.cxf.jaxb.managemerchant.RequestMerchantRegistrationType;
import com.ap.core.cxf.jaxb.managemerchant.ResponseMerchantRegistrationType;
import com.ap.core.cxf.jaxb.msghdr.MessageType;
import com.ap.core.jbehave.BaseRTPSteps;
import com.ap.core.jbehave.StoryContext;
import com.ap.core.util.JaxbUtil;
import com.ap.generator.registermerchant.RequestManageMerchantGeneratorBuilder;
import com.core.ap.wsdl.managemerchantservice_1.ManageMerchant1PortType;
import com.core.ap.wsdl.managemerchantservice_1.RegisterMerchant1Fault;

public class ManageMerchantSteps extends BaseRTPSteps {

    private RequestMerchantRegistrationType requestMerchantRegistrationType;
    private ResponseMerchantRegistrationType responseMerchantRegistrationType;
    /**
     * @param storyContext
     */
    public ManageMerchantSteps(StoryContext storyContext) {
        super(storyContext);
    }

    @Given("that the Distributor has been on-boarded and is registered for AP Service")
    public void givenDistributorOnBoarded() {
    }

    @Given("a merchant requests their Distributor for ZAPP enrolment")
    public void givenMerchantRequestedForZappEnrolment() {
    }

    @Given("with RequestType as REGISTER")
    public void givenWithRequestTypeAsREGISTER() throws IOException {
        requestMerchantRegistrationType = RequestManageMerchantGeneratorBuilder.getRequestManageMerchantGeneratorBuilder().build().generate();
        requestMerchantRegistrationType.getMerchant().setRequestType(RequestTypeCode.REGISTER);
        final Resource resource = new ClassPathResource("data/image.jpg");
        byte[] bytes = IOUtils.toByteArray(resource.getInputStream());
        requestMerchantRegistrationType.getMerchant().setMerchantLogo(bytes);
        JaxbUtil.newInstance().javaToXML(RequestMerchantRegistrationType.class, requestMerchantRegistrationType);
    }

    @Given("Status of the Distributor is validated to be ACTIVE or PNGACTIVE")
    public void givenStatusOfTheDistributorIsValidatedToBeACTIVEOrPNGACTIVE() {
        requestMerchantRegistrationType.setDistributorId("000001");
    }

    @When("Distributor requests (valid and correct form) to AP Service to add merchant to their ZAPP Profile/account")
    public void whenDistributorRequestsToAPServiceToAddMerchantToTheirZAPPProfileAccount() throws RegisterMerchant1Fault {
        final ManageMerchant1PortType manageMerchant1PortType = factory.getBean(ManageMerchant1PortType.class);
        try {
        responseMerchantRegistrationType = manageMerchant1PortType.registerMerchant1(requestMerchantRegistrationType);
        } catch(SOAPFaultException e)
        {
        	System.out.println("dilip................................."+e.getFault().getTextContent());
        	e.printStackTrace();
        }
        JaxbUtil.newInstance().javaToXML(ResponseMerchantRegistrationType.class, responseMerchantRegistrationType);
    }

    @Then("AP Service updates databases with ACTIVE status for merchant")
    public void thenAPServiceUpdatesDatabasesWithACTIVEStatusforMerchant() {
    }

    @Then("AP Service sends response to Distributor with UpdateStatus as Successful")
    public void thenAPServiceSendsResponseToDistributorWithUpdateStatusAsSuccessful() {
        assertThat("Message Type  is not as expected", true, CoreMatchers.is(equalTo(responseMerchantRegistrationType.getMsgHdr().getMsgType() == MessageType.ACKADDMERCHANT)));
        assertThat("Original message Id  is not as expected", true, CoreMatchers.is(equalTo(responseMerchantRegistrationType.getMsgHdr().getOrgReqMsgId().equals(requestMerchantRegistrationType.getMsgHdr().getMsgId()))));
        assertThat("Activation Status  is not as expected", true, CoreMatchers.is(equalTo(responseMerchantRegistrationType.getMerchantRegistResponse().getActivationStatus() == ActivationStatus.ACTIVE)));
        assertThat("Update Status  is not as expected", true, CoreMatchers.is(equalTo(responseMerchantRegistrationType.getMerchantRegistResponse().getUpdateStatus() == UpdateStatus.SUCCESSFUL)));

    }

}
